package edu.nf.controller.room;

import edu.nf.controller.BaseController;
import edu.nf.hotel.vo.ResultVO;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * @Data 2023-06-25
 * @Author Lsw
 */
public class DeleteProductImageController extends BaseController {
    private static final String DEFAULT_IMAGE = "default.jpg" ;

    @RequestMapping("/deleteImage")
    public View deleteImage(@RequestParam("fileName") String fileName, HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        String deleteImage = req.getParameter("last_image");

        // 2.获取图片上传的物理路径
        String path = req.getServletContext().getRealPath("/images/room");

        // 3.删除图片
        if (deleteImage != null && !"".equals(deleteImage)) {
            File f = new File(path + File.separator + deleteImage);
            if (f.exists() && !DEFAULT_IMAGE.equals(deleteImage)) {
                f.delete();
            }
        }
        ResultVO vo = success(fileName);
        vo.setMessage("删除成功");
        vo.setCode(200);
        return new JsonView(vo);

    }
}
